﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;

namespace ClassesForFtpCrawler
{
    class DatabaseAccess
    {
        public string GetEncodedFileFtpPath(int mediaId)
        {
            using (var context = this.GetReadDataContext())
            {
                var encodeInfo = (from m in context.tbMediaReplications
                                  where m.MediaId == mediaId
                                  select m).First();

                var ftpConnectionStringUser = new ConnectionStringUser(encodeInfo.tbMediaReplicationConnection.ConnectionString);

                var ftpPath = ftpConnectionStringUser.DataSource + "/" +
                    encodeInfo.Destination;

                return ftpPath;
            }
        }

       public string GetSourceFileFtpPath(int mediaId)
        {
            string ftpPath = "";
            using (var context = this.GetReadDataContext())
            {
                var fileInfo = (from m in context.tbMedias
                                where m.id == mediaId
                                select m).First();
                // server in tbFileLocation is an http server - convert it to the ftp equivalent.
                // http server looks like 'http://mcne-00.vo.llnwd.net/s/d1/' 
                // need to convert it to 'ftp://mcne.upload.llnw.net/s/'
                var server = fileInfo.tbFileLocation.Server;
                Regex serverNumberPattern = new Regex("/d[0-9]/");
                var match = serverNumberPattern.Match(server);
                if (match.Success)
                {
                    server = server.Replace(match.Value, "//"); // take the d1 off the end
                    // remove 'http://mcne-00.vo.llnwd', replace with 'ftp://mcne[-num].upload.llnw'
                    var serverNumber = Regex.Match(match.Value, "[0-9]");
                    if (match.Success)
                    {
                        if (Int32.Parse(serverNumber.Value) > 1)
                        {
                            server = server.Replace("http://mcne-00.vo.llnwd", "ftp://mcne-" + serverNumber.Value + ".upload.llnw");
                        }
                        else
                        {
                            server = server.Replace("http://mcne-00.vo.llnwd", "ftp://mcne.upload.llnw");
                        }
                    }
                }

                ftpPath =  server +
                    fileInfo.tbFileLocation.BaseFolder.Replace("\\", "/") + "/" + 
                    fileInfo.Folder + "/" +
                    fileInfo.BaseFile;

                return ftpPath;
            }
        }
        
        // get a connection to the database that uses the data models defined in DataClasses.dbml
        private DataClassesDataContext GetReadDataContext()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
            var c = new DataClassesDataContext(connectionString);
            c.ObjectTrackingEnabled = false;
            c.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
            return c;
        }
    }
}
